﻿using System;
using System.Collections.Generic;
using System.Linq;
using Anchor.Miscellaneous;
using Anchor.Sockets;
using Anchor;

namespace Anchor
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                new Environment().Boot();
                Console.ReadLine();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
    }
    public class Environment
    {
        private static DatabaseManager DatabaseManager;
        private Bootup mBoot = new Bootup();
        private static ConfigurationModule mConfig;
        public static ConfigurationModule Configuration
        {
            get { return mConfig; }
        }
        public void Boot()
        {
            new Logging().Title("Anchor: Habbo Hotel");

            new Logging().WriteLine("######################", ConsoleColor.Gray);
            new Logging().WriteLine("## Anchor Framework ##", ConsoleColor.Gray);
            new Logging().WriteLine("######################\n", ConsoleColor.Gray);

            try
            {
                mConfig = ConfigurationModule.LoadFromFile("settings.ini");
            }
            catch (System.IO.FileNotFoundException ex)
            {
                Console.WriteLine("Failed to load configuration file, exception message was: " + ex.Message);
                return;
            }

            DatabaseServer pDatabaseServer = new DatabaseServer(
                Environment.Configuration["db1.server.host"],
                Environment.Configuration.TryParseUInt32("db1.server.port"),
                Environment.Configuration["db1.server.uid"],
                Environment.Configuration["db1.server.pwd"]);

            Database pDatabase = new Database(
                Environment.Configuration["db1.name"],
                Environment.Configuration.TryParseUInt32("db1.minpoolsize"),
                Environment.Configuration.TryParseUInt32("db1.maxpoolsize"));

            DatabaseManager = new DatabaseManager(pDatabaseServer, pDatabase);

            using (DatabaseClient client = GetDatabase().GetClient())
            {
                client.ExecuteQuery("UPDATE users SET SockId = '0'");
            }


            mBoot.Boot();

            //Initalize the socket
            new SocketListener().Invoke();
        }
        public static DatabaseManager GetDatabase()
        {
            return DatabaseManager;
        }
        public Bootup getPackets()
        {
            return mBoot;
        }
    }
    
}
